var addMerchandise = new Vue({
    el: '#addMerchandise',
    //初始化数据类型
    data: {
        //表单提交路劲
        importFileUrl: "/home/commodity/insertCommodity",
        //已经上传的图片
        // fileList:[{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        //横向菜单列表
        goodsType: [],
        //商品类型
        category: [],
        selectedOptions: [],
        //所属商家信息
        merchant: {},
        //所属商家ID
        merchantId: '',
        //判断该页面商品修改与添加
        type:'',
        //页面数据
        commodity: {
            category: '',
            commodityName: '',
            //品牌
            brand: '',
            //供货价
            commodityMakPrc: '',
            //市场价
            commodityTrcPrc: '',
            //库存
            stock: '',
            //其他说明
            rests: '',
            //型号
            model: '',
            //商品代码
            num: '',
            //质保时间
            quality: '',
            //制造商
            manufacturer: '',
            //商品详细信息
            details: ''
        },
        //上传数据信息
        map: {},
        //添加或者修改,没有数据为添加，有数据为修改
        addModify: '',
        //上传文件单位名称
        fileName:''


    },
    //时间提交方法
    methods: {
        //添加商品信息
        addCommodity: function () {
            debugger
            var _this = this;
            var optionMap = _this.selectedOptions;
            var value = [];
            //循环取值
            for (var key in optionMap) {
                value.push(optionMap[key]);
            }
            //在每个取到的值之间加“-”
            var str = value.join('-');
            _this.commodity.category = str;

            _this.commodity.details = UE.getEditor('editor').getContent();

            if (_this.addModify != '') {
                $.post("/home/commodity/updateCommodity",
                    {"commodity": JSON.stringify(_this.commodity),},
                    function (data) {
                        if (data.code == 200) {

                            _this.$notify({
                                message: "商品信息修改成功!",
                                type: 'success'
                            });

                        } else {
                            _this.$notify({
                                message: '商品信息修改失败!请您检查表单输入信息后重新提交',
                                type: 'error'
                            });
                        }


                    });

            } else {
                _this.commodity.businessesId = _this.merchant.id;
                _this.commodity.businessesName = _this.merchant.companyName;
                _this.map.commodity = JSON.stringify(_this.commodity);
                _this.map.url = _this.merchant.institutionCode;
                _this.map.addModify = _this.addModify;
                _this.$refs.upload.submit();
            }

            $("#submit").attr("disabled","disabled");
        },
        //添加HTML信息到编辑框中
        initializeEdit: function (details) {
            debugger
            setTimeout(function () {
                UE.getEditor('editor').execCommand('insertHtml', details);

            }, 200);

        },
        //文件上传成功后的回调函数
        handleAvatarSuccess:function(response, file, fileList) {
            this.$message.success(response);
        },
        //文件上传前的回调函数
        beforeAvatarUpload:function(file){
            var isLt2M = file.size / 1024 / 1024 < 2;

            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return  isLt2M;

        },
        //文件上传失败的回调函数
        handleAvatarError:function(err, file, fileList){
            this.$message.error("失败！"+err);
        },

    },
    //页面加载完成后执行此方法
    created: function () {
        var _this = this;
        $.post("/home/menu/select", "", function (data) {
            //循环第一级菜单
            for (var i = 0; i < data.data.Menu.length; i++) {
                var map1 = {};
                map1.children = [];
                map1.value = data.data.Menu[i].id;
                map1.label = data.data.Menu[i].name;
                //循环第二级菜单
                for (var j = 0; j < data.data.Menu[i].childMenus.length; j++) {
                    var map2 = {};
                    map2.value = data.data.Menu[i].childMenus[j].id;
                    map2.label = data.data.Menu[i].childMenus[j].name;
                    map2.children = [];
                    //循环第三菜单
                    for (var z = 0; z < data.data.Menu[i].childMenus[j].childMenus.length; z++) {
                        var map3 = {};
                        map3.value = data.data.Menu[i].childMenus[j].childMenus[z].id;
                        map3.label = data.data.Menu[i].childMenus[j].childMenus[z].name;
                        map3.type = data.data.Menu[i].childMenus[j].childMenus[z].coding;
                        map2.children.push(map3);
                    }
                    map1.children.push(map2);

                }
                // debugger
                _this.category.push(map1);
            }

        });
        $.post("/home/commodity/selectMerchant",
            '',
            function (data) {
                var _this = this;
                //初始化商家
                for (var i = 0; i < data.data.merchantList.length; i++) {
                    var map = {};
                    map.label = data.data.merchantList[i].companyName;
                    map.value = data.data.merchantList[i].id;
                    _this.merchantList.push(map);
                }
            });
        var goodsTypeList = [
            {href: "../home/designatedService.html", name: "定点服务", url: "#"},
            {href: "../home/bidding.html", name: "在线询价", url: "#"}
        ];
        _this.goodsType = goodsTypeList;//后台动态添加进入

    }
});